package com.xyazm.order.statemachine.action.order.create;

import com.xyazm.common.enums.OrderStatusChangeEnum;
import com.xyazm.order.domain.builder.FullOrderData;
import com.xyazm.order.domain.dto.OrderInfoDTO;
import com.xyazm.order.domain.dto.SubOrderCreateDTO;
import com.xyazm.order.statemachine.action.OrderStateAction;
import com.xyazm.process.engine.model.ProcessContextFactory;
import com.xyazm.process.engine.process.ProcessContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 创建子订单Action
 * @author xuyou
 * @date 2023年02月04日 21:55
 */
@Slf4j
@Component
public class SubOrderCreateAction extends OrderStateAction<SubOrderCreateDTO> {

    @Resource
    private ProcessContextFactory processContextFactory;

    @Override
    public OrderStatusChangeEnum event() {
        return OrderStatusChangeEnum.SUB_ORDER_CREATED;
    }

    @Override
    protected OrderInfoDTO onStateChangeInternal(OrderStatusChangeEnum event, SubOrderCreateDTO context) {
        FullOrderData fullMasterOrderData = context.getFullMasterOrderData();
        Integer productType = context.getProductType();
        // 获取流程引擎并执行
        ProcessContext subOrderCreateProcess = processContextFactory.getContext("subOrderCreateProcess");
        subOrderCreateProcess.set("fullMasterOrderData", fullMasterOrderData);
        subOrderCreateProcess.set("productType", productType);
        subOrderCreateProcess.start();
        // 流程执行完之后，获取返回参数
        return subOrderCreateProcess.get("orderInfoDTO");
    }

}
